﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ProductSystem.BLL.Common
{
    public class AppUserSession
    {
        private static object locker = new object();
        private static AppUserSession instance;

        public static AppUserSession Instance()
        {
            if (null == instance)
            {
                lock (locker)
                {
                    if (null == instance)
                    {
                        instance = new AppUserSession();
                    }
                }
            }
            return instance;
        }

        /// <summary>
        /// 设置企业用户登录Session
        /// </summary>
        /// <param name="currentUser"></param>
        /// <returns></returns>
        public bool SetCurrentUser(LoginUser currentUser)
        {
            bool success = false;
            HttpContext context = HttpContext.Current;

            try
            {
                if (context != null && context.Session != null)
                {
                    context.Session[SessionContants.UserInfoSession] = currentUser;
                    success = true;
                }
            }
            catch (Exception ex)
            {
                success = false;
            }

            return success;
        }

        /// <summary>
        /// 获取用户登录Session
        /// </summary>
        /// <returns></returns>
        public LoginUser GetCurrentUser()
        {
            HttpContext context = HttpContext.Current;
            LoginUser currentUser = null;
            try
            {
                if (context != null && context.Session != null)
                {
                    currentUser = context.Session[SessionContants.UserInfoSession] as LoginUser;
                }
            }
            catch (Exception ex)
            {
            }

            return currentUser;
        }




        public bool SetReturnUrl(String returnUrl)
        {
            bool success = false;
            HttpContext context = HttpContext.Current;

            try
            {
                if (context != null && context.Session != null)
                {
                    context.Session[SessionContants.UserReturnUrl] = returnUrl;
                    success = true;
                }
            }
            catch (Exception ex)
            {
                success = false;
            }

            return success;
        }

        public String GetReturnUrl()
        {
            HttpContext context = HttpContext.Current;
            String returnUrl = null;
            try
            {
                if (context != null && context.Session != null)
                {
                    returnUrl = context.Session[SessionContants.UserReturnUrl] as string;
                }
                else
                {
                    returnUrl = "HomePage";
                }
            }
            catch (Exception ex)
            {
            }

            return returnUrl;
        }


        public bool SetReturnUrlParam()
        {
            bool success = false;
            HttpContext context = HttpContext.Current;
            try
            {
                if (context != null && context.Session != null)
                {
                    string urlReferrer = context.Request.UrlReferrer.ToString();
                    if (urlReferrer.Contains('?'))
                    {
                        context.Session[SessionContants.UserReturnUrlParam] = urlReferrer.Substring(urlReferrer.LastIndexOf('?'));
                    }
                    else
                    {
                        context.Session[SessionContants.UserReturnUrlParam] = "";
                    }

                    success = true;
                }
            }
            catch (Exception ex)
            {
                return success;
            }
            return success;
        }


        public string GetReturnUrlParam()
        {
            HttpContext context = HttpContext.Current;
            string returnUrlParam = null;
            try
            {
                if (context != null && context.Session != null)
                {
                    returnUrlParam = context.Session[SessionContants.UserReturnUrlParam] as string;
                }
                else
                {
                    returnUrlParam = "";
                }
            }
            catch (Exception ex)
            {
            }
            return returnUrlParam;
        }
    }
}
